﻿Imports Microsoft.VisualBasic
Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Imports AugmentLib.WebComponent
Imports System.Collections.Generic
Imports System

Namespace DTO
    Public Class CollectionItemDetail
        Private _id As Double
        Private _transactionId As String
        Private _amount As Decimal
        Private _collectionType As String
        Private _collectdate As Date
        Private _remark As String
        Private _transactor As String

        Public Property Id() As Double
            Get
                Return _id
            End Get
            Set(ByVal value As Double)
                _id = value
            End Set
        End Property

        Public Property TransactionId() As String
            Get
                Return _transactionId
            End Get
            Set(ByVal value As String)
                _transactionId = value
            End Set
        End Property
        Public Property Amount() As Decimal
            Get
                Return _amount
            End Get
            Set(ByVal value As Decimal)
                _amount = value
            End Set
        End Property
        Public Property Collectdate() As Date
            Get
                Return _collectdate
            End Get
            Set(ByVal value As Date)
                _collectdate = value
            End Set
        End Property
        Public Property CollectionType() As String
            Get
                Return _collectionType
            End Get
            Set(ByVal value As String)
                _collectionType = value
            End Set
        End Property
        Public Property Remark() As String
            Get
                Return _remark
            End Get
            Set(ByVal value As String)
                _remark = value
            End Set
        End Property

        Public Property Transactor() As String
            Get
                Return _transactor
            End Get
            Set(ByVal value As String)
                _transactor = value
            End Set
        End Property

    End Class
End Namespace


Namespace Impl
    Public Class SaleDetailCollection

        Public Sub New()

        End Sub

        Public Function retrieve(Optional ByVal transactionId As String = "1000", Optional ByVal transactType As String = "Sale") As List(Of DTO.CollectionItemDetail)
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim TempDT As New Data.DataTable
            Dim c As New List(Of DTO.CollectionItemDetail)

            Try
                objDB.OpenConnection()

                If (transactType.Equals("Sale")) Then
                    objDB.dbVariable.SQLString = "SELECT t.[TransactionId] as TransactionId " & _
                        ",t.[CollectAmt] as CollectAmt " & _
                        ",t.[CollectCompleteBy] as tCollectCompleteBy " & _
                        ",t.[CollectCompleteDate] as CollectCompleteDate " & _
                        ",t.[ID] as ID,t.[Remark] as Remark " & _
                        ",t.[CollectionType] as [CollectionType] " & _
                        ",cs.Name as CollectCompleteBy, cs.Initial as CollectCompleteByInitial " & _
                        "FROM [AW_SaleTransactionCollection] t " & _
                        "LEFT OUTER JOIN AW_Staff cs ON t.CollectCompleteBy = cs.StaffId " & _
                        "WHERE t.[TransactionId]=@TransactionId"
                    objDB.AddParameter("TransactionId", Data.SqlDbType.UniqueIdentifier, New Guid(transactionId.Trim()))
                    objDB.FillData(TempDT, "AW_SaleTransaction", Database.SQLCommandType.NormalString)
                Else
                    objDB.dbVariable.SQLString = "SELECT t.[TransactionId] as TransactionId " & _
                        ",t.[CollectAmt] as CollectAmt " & _
                        ",t.[CollectCompleteBy] as tCollectCompleteBy " & _
                        ",t.[CollectCompleteDate] as CollectCompleteDate " & _
                        ",t.[ID] as ID,t.[Remark] as Remark " & _
                        ",t.[CollectionType] as [CollectionType] " & _
                        ",cs.Name as CollectCompleteBy, cs.Initial as CollectCompleteByInitial " & _
                        "FROM [AW_RentTransactionCollection] t " & _
                        "LEFT OUTER JOIN AW_Staff cs ON t.CollectCompleteBy = cs.StaffId " & _
                        "WHERE t.[TransactionId]=@TransactionId"
                    objDB.AddParameter("TransactionId", Data.SqlDbType.UniqueIdentifier, New Guid(transactionId.Trim()))
                    objDB.FillData(TempDT, "AW_RentTransaction", Database.SQLCommandType.NormalString)
                End If



                For Each ReadRow As Data.DataRow In TempDT.Rows
                    Dim itemdetail = New DTO.CollectionItemDetail
                    itemdetail.TransactionId = ReadRow.Item("TransactionId").ToString
                    If (IsDBNull(ReadRow.Item("CollectAmt")) = False) Then
                        itemdetail.Amount = ReadRow.Item("CollectAmt").ToString
                    Else
                        itemdetail.Amount = 0
                    End If
                    If (IsDBNull(ReadRow.Item("CollectCompleteDate")) = False) Then
                        itemdetail.Collectdate = Convert.ToDateTime(ReadRow.Item("CollectCompleteDate"))
                    Else
                    End If

                    If (IsDBNull(ReadRow.Item("ID")) = False) Then
                        itemdetail.Id = ReadRow.Item("ID").ToString
                    Else
                        itemdetail.Id = 0
                    End If

                    If (IsDBNull(ReadRow.Item("CollectCompleteBy")) = False) Then
                        itemdetail.Transactor = ReadRow.Item("CollectCompleteBy").ToString()
                    Else
                        itemdetail.Transactor = ""
                    End If
                    If (IsDBNull(ReadRow.Item("CollectionType")) = False) Then
                        itemdetail.CollectionType = ReadRow.Item("CollectionType")
                    Else
                        itemdetail.CollectionType = ""
                    End If
                    If (IsDBNull(ReadRow.Item("Remark")) = False) Then
                        itemdetail.Remark = ReadRow.Item("Remark").ToString()
                    Else
                        itemdetail.Remark = "Nil"
                    End If

                    c.Add(itemdetail)
                Next

                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try

            Return c
        End Function

        Public Sub updateCollection(ByVal Id As String, ByVal TransactionId As String, ByVal CollectionType As String, ByVal Collectdate As DateTime, ByVal Amount As String, ByVal Remark As String, ByVal transactor As String, ByVal transactType As String)
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim TempDT As New Data.DataTable
            Try
                objDB.OpenConnection()

                If (Amount.Equals("")) Then Amount = "0"
                If (transactType.Equals("Sale")) Then
                    objDB.dbVariable.SQLString = "UPDATE AW_SaleTransactionCollection SET CollectAmt=@CollectAmt,CollectCompleteBy=@CollectCompleteBy,CollectCompleteDate=@CollectCompleteDate, Remark=@Remark,CollectionType=@CollectionType WHERE ID=@ID"
                Else
                    objDB.dbVariable.SQLString = "UPDATE AW_RentTransactionCollection SET CollectAmt=@CollectAmt,CollectCompleteBy=@CollectCompleteBy,CollectCompleteDate=@CollectCompleteDate, Remark=@Remark,CollectionType=@CollectionType WHERE ID=@ID"
                End If

                objDB.AddParameter("@TransactionId", Data.SqlDbType.UniqueIdentifier, New Guid(TransactionId))
                objDB.AddParameter("@ID", Data.SqlDbType.Int, Id)
                objDB.AddParameter("@CollectAmt", Data.SqlDbType.Decimal, Amount)
                objDB.AddParameter("@CollectCompleteBy", Data.SqlDbType.UniqueIdentifier, New Guid(transactor))
                objDB.AddParameter("@CollectionType", Data.SqlDbType.VarChar, CollectionType)
                objDB.AddParameter("@CollectCompleteDate", Data.SqlDbType.DateTime, Collectdate)
                objDB.AddParameter("@Remark", Data.SqlDbType.VarChar, Remark)

                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End Sub

        Public Sub insert(ByVal TransactionId As String, ByVal CollectionType As String, ByVal Collectdate As DateTime, ByVal Amount As String, ByVal Remark As String, ByVal transactor As String, ByVal transactType As String)
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim TempDT As New Data.DataTable
            Try
                objDB.OpenConnection()

                If (Amount.Equals("")) Then Exit Sub
                If (transactType.Equals("Sale")) Then
                    objDB.dbVariable.SQLString = "INSERT INTO AW_SaleTransactionCollection (TransactionId,CollectAmt,CollectCompleteBy,CollectCompleteDate,Remark,CollectionType) VALUES (@TransactionId,@CollectAmt,@CollectCompleteBy,@CollectCompleteDate, @Remark,@CollectionType)"
                Else
                    objDB.dbVariable.SQLString = "INSERT INTO AW_RentTransactionCollection (TransactionId,CollectAmt,CollectCompleteBy,CollectCompleteDate,Remark,CollectionType) VALUES (@TransactionId,@CollectAmt,@CollectCompleteBy,@CollectCompleteDate, @Remark,@CollectionType)"
                End If

                objDB.AddParameter("@TransactionId", Data.SqlDbType.UniqueIdentifier, New Guid(TransactionId))
                objDB.AddParameter("@CollectAmt", Data.SqlDbType.Decimal, Amount)
                objDB.AddParameter("@CollectCompleteBy", Data.SqlDbType.UniqueIdentifier, New Guid(transactor))
                objDB.AddParameter("@CollectionType", Data.SqlDbType.VarChar, CollectionType)
                objDB.AddParameter("@CollectCompleteDate", Data.SqlDbType.DateTime, Collectdate)
                If Remark Is Nothing Then
                    Remark = ""
                End If
                objDB.AddParameter("@Remark", Data.SqlDbType.VarChar, Remark)
                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End Sub

        Public Sub delete(ByVal Id As String, ByVal transactType As String)
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim TempDT As New Data.DataTable

            Try
                objDB.OpenConnection()
                If (transactType.Equals("Sale")) Then
                    objDB.dbVariable.SQLString = "DELETE FROM AW_SaleTransactionCollection WHERE ID=@ID"
                Else
                    objDB.dbVariable.SQLString = "DELETE FROM AW_RentTransactionCollection WHERE ID=@ID"
                End If

                objDB.AddParameter("@ID", Data.SqlDbType.Int, Id)
                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End Sub
    End Class
    Public Class LoginCollection

        Public Function retrieve(ByVal noRecord As Integer) As Data.DataTable
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim retTable As New Data.DataTable
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "SELECT TOP " & noRecord.ToString & " LoginName,LoginDate,LoginStatus FROM AW_Login order by LoginDate desc"
                objDB.FillData(retTable, "AW_Login", Database.SQLCommandType.NormalString)
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            Return retTable

        End Function

        Public Function retrieveInvalidHost(ByVal noRecord As Integer) As Data.DataTable
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim retTable As New Data.DataTable
            Try
                objDB.OpenConnection()

                objDB.dbVariable.SQLString = "SELECT TOP " & noRecord.ToString & " InvalidLatitude,InvalidLongitude,CreatedDate FROM AW_InvalidHost order by CreatedDate desc"
                objDB.FillData(retTable, "AW_InvalidHost", Database.SQLCommandType.NormalString)
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            Return retTable
        End Function

    End Class
End Namespace

Namespace WS
    Public Class PropertyDetailDTO
        Private _StatusProcess As String
        Private _StatusProcessTotal As String

        Public Property StatusProcess() As String
            Get
                Return _StatusProcess
            End Get
            Set(ByVal value As String)
                _StatusProcess = value
            End Set
        End Property
        Public Property StatusProcessTotal() As String
            Get
                Return _StatusProcessTotal
            End Get
            Set(ByVal value As String)
                _StatusProcessTotal = value
            End Set
        End Property
    End Class
End Namespace
